package cn.summit.core.config;

import cn.summit.core.authentication.authorize.AuthorizeConfigurerManager;
import cn.summit.core.authentication.authorize.AuthorizeConfigurerProvider;
import cn.summit.core.authentication.authorize.CustomAuthorizeConfigurerProvider;
import cn.summit.core.authentication.authorize.CutomAuthorizeConfigurerManager;
import cn.summit.core.authentication.handler.CustomInvalidSessionHandler;
import cn.summit.core.authentication.handler.CustomSessionInformationExpiredHandler;
import cn.summit.core.mobile.DefaultSmsSender;
import cn.summit.core.mobile.SmsSend;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.web.session.InvalidSessionStrategy;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

/**
 * 默认bean注入，如果已经实现则不注入
 *
 * @author summit
 * @since 2020/3/4 21:15
 */
@Configuration
public class SecurityBeanConfig {


    /**
     * 短信发送接口 如果已经实现SmsSend，则忽略
     *
     * @return bean
     */
    @Bean
    @ConditionalOnMissingBean(SmsSend.class)
    public SmsSend smsSend() {
        return new DefaultSmsSender();
    }

    // /**
    //  * 当session失效后的处理类
    //  * @return
    //  */
    // @Autowired
    // private SessionRegistry sessionRegistry;

    /**
     * session超时处理
     *
     * @return 处理
     */
    @Bean
    @ConditionalOnMissingBean(InvalidSessionStrategy.class)
    public InvalidSessionStrategy InvalidSessionHandler(SessionRegistry sessionRegistry) {
        return new CustomInvalidSessionHandler(sessionRegistry);
    }


    @Bean
    @ConditionalOnMissingBean(SessionInformationExpiredStrategy.class)
    public SessionInformationExpiredStrategy sessionInformationExpiredStrategy() {
        return new CustomSessionInformationExpiredHandler();
    }

    @Bean
    @Order(Integer.MAX_VALUE)//越大加载越后
    public AuthorizeConfigurerProvider authorizeConfigurerProvider() {
        return new CustomAuthorizeConfigurerProvider();
    }


    @Bean
    public AuthorizeConfigurerManager authorizeConfigurerManager() {
        return new CutomAuthorizeConfigurerManager();
    }

}
